﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using TableTools.Kits;

namespace HangerSupport
{
	internal class HSResultDraw
	{
		public HSResultDraw(UIApplication uiApp)
		{
			this.m_uiApp = uiApp;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_selection = this.m_uiApp.ActiveUIDocument.Selection;
		}

		public void DrawCell(XYZ ptLeftTop, double width, double height, string strText)
		{
			this.DrawText(ptLeftTop, width, height, strText);
			XYZ direction = XYZ.BasisX * width;
			XYZ direction2 = -XYZ.BasisY * height;
			this.DrawModelLine(ptLeftTop, direction);
			this.DrawModelLine(ptLeftTop, direction2);
		}

		public void DrawModelLine(XYZ ptStart, XYZ direction)
		{
			XYZ xyz = ptStart + direction;
			Line line = Line.CreateBound(ptStart, xyz);
			Autodesk.Revit.DB.View activeView = this.m_uiApp.ActiveUIDocument.ActiveView;
			DetailCurve detailCurve = this.m_uiApp.ActiveUIDocument.Document.Create.NewDetailCurve(activeView, line);
			bool flag = detailCurve != null;
			if (flag)
			{
				this.m_SubElementsList.Add(detailCurve.Id);
			}
		}

		private void DrawText(XYZ ptLeftTop, double width, double height, string text)
		{
			bool flag = text.CompareTo("") == 0;
			if (!flag)
			{
				StringComposingKit stringComposingKit = new StringComposingKit(text, width, height * 0.5);
				double num = 0.0;
				double num2 = 0.0;
				List<string> list = new List<string>();
				stringComposingKit.ComosingHorizontalAndVertical(ref num, ref num2, ref list);
				num *= 0.4;
				double num3 = height / (double)list.Count;
				for (int i = 0; i < list.Count; i++)
				{
					string text2 = list[i];
					double num4 = ptLeftTop.Y - (double)i * num3 - 0.6 * num3;
					XYZ origin = new XYZ(ptLeftTop.X + width / 2.0, num4, 0.0);
					double lineWidth = num * num2 * (double)(text2.Length + 2);
					lineWidth = 0.1;
					TextNote textNode = this.m_uiApp.ActiveUIDocument.Document.GetTextNode(this.m_uiApp.ActiveUIDocument.ActiveView, origin, new XYZ(1.0, 0.0, 0.0), new XYZ(0.0, 1.0, 0.0), lineWidth, (Autodesk.Revit.DB.TextAlignFlags)1152, text2);
					bool flag2 = textNode != null;
					if (flag2)
					{
						this.m_SubElementsList.Add(textNode.Id);
					}
				}
			}
		}

		public Autodesk.Revit.DB.Group CreateGroup()
		{
			ElementSet elementSet = new ElementSet();
			foreach (ElementId id in this.m_SubElementsList)
			{
				Element element = RevitVersionFuncs.GetElement(this.m_doc, id);
				elementSet.Insert(element);
			}
			Autodesk.Revit.DB.Group result;
			try
			{
				Autodesk.Revit.DB.Group group = null;
				bool flag = elementSet.Size > 0;
				if (flag)
				{
					group = RevitVersionFuncs.NewGroup(this.m_doc, elementSet);
				}
				group.GroupType.Name = "TableGroup";
				result = group;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = null;
			}
			return result;
		}

		private UIApplication m_uiApp;

		private Document m_doc;

		private Selection m_selection;

		private List<ElementId> m_SubElementsList = new List<ElementId>();
	}
}
